package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/gomodule/redigo/redis"
	"root/consts"
	"root/utility"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"fmt"
	"strconv"
)

type Data struct {

	H5Url string `json:"h5_url"`
	InvateCode string `json:"invate_code"`
	Content string `json:"content"`

}

type Token struct {
	Token string `json:"token"`
}
func GetShareUrl(c *gin.Context)  {

	conf ,err := redis.String(utility.GetRedisConn().Do("GET",consts.SYSTEM_CONFIG))
	if(err != nil){
		SendResponse(c,400,"",nil)
		return
	}

	var config map[string]interface{}
	config,err = utility.JsonToMap(conf)
	if err != nil {
		SendResponse(c,400,"",err)
		return

	}
	//fmt.Println(c.GetString("uid"))
	//fmt.Println(c.GetInt("uid"))
	//fmt.Println(c.MustGet("uid"))
	d := Data{}
	d.H5Url 		= config["share_url"].(string)
	d.InvateCode 	= c.GetString("uid")
	d.Content 		= config["reward_content"].(string)
	//
	SendResponse(c,200,"",d)
	return


}

func GetUploadToken(c *gin.Context)  {

	var config map[string]interface{}
	conf ,err := redis.String(utility.GetRedisConn().Do("GET",consts.SYSTEM_CONFIG))
	if(err != nil){
		SendResponse(c,400,"",nil)
		return
	}
	config,err = utility.JsonToMap(conf)
	if err != nil {
		SendResponse(c,400,"",err)
		return

	}

	accessKey := config["qiniu_ak"].(string)
	secretKey := config["qiniu_sk"].(string)
	bucket := config["qiniu_bucket"].(string)
	expires :=  config["qiniu_expires"].(string)
	Expires,err:= strconv.Atoi(expires)
	fmt.Println(Expires)
	putPolicy := storage.PutPolicy{
		Scope: bucket,
	}
	putPolicy.Expires = uint64(Expires)
	mac := qbox.NewMac(accessKey, secretKey)
	upToken := putPolicy.UploadToken(mac)
	Token := Token{}
	Token.Token = upToken
	SendResponse(c,200,"",Token)
}